//
//  SortView.m
//  dzm
//
//  Created by dzmmac on 15/4/10.
//  Copyright (c) 2015年 dzmmac. All rights reserved.
//

#import "SortView.h"
#import "AppConfig.h"
#import "AppColorConfig.h"
#import "UIView+Additions.h"
#import "AppDelegate.h"
#import "UIScreen+Frame.h"
@implementation SortView


-(id)init
{
    
    self.sortTableView = [[UITableView alloc ] init];
    self.sortTableView.delegate = self;
    self.sortTableView.dataSource = self;
    
    self.sortTableView.scrollsToTop = NO;
    self.sortTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.sortTableView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.sortTableView.showsVerticalScrollIndicator = NO;
    self.sortTableView.pagingEnabled = YES;
    self.sortTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.bgView = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, [UIScreen width], [UIScreen height])];
    self.bgView.backgroundColor = [UIColor blackColor];
    self.bgView.userInteractionEnabled = YES;
    
    self.tap = [[UITapGestureRecognizer alloc ] initWithTarget:self action:@selector(closeBtnAction)];
    [self.bgView addGestureRecognizer:self.tap];
    
    return [super init];
}

-(void)layoutSubviews
{
    if ([self.sortTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.sortTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.sortTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.sortTableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    [super layoutSubviews];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rowCount = self.dataList.count;
   
    
    return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"ViewCell";
    UITableViewCell *cell = nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ] ;
        cell.contentView.backgroundColor=[UIColor clearColor];
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        UILabel *leftLabel = [[UILabel alloc ] initWithFrame:CGRectMake(10, 0,[UIScreen width] - 20 , 44)];
        leftLabel.font = [UIFont systemFontOfSize:14];
        
        
        leftLabel.tag = 10;
        [cell.contentView addSubview:leftLabel];
        leftLabel.text = self.dataList[indexPath.row];
        
        
        if (self.sortType == 1) {
            leftLabel.textColor = UI_RGBA(51, 51, 51, 1);
            leftLabel.textAlignment = NSTextAlignmentCenter;
        }else if(self.sortType == 0)
        {
            leftLabel.textColor = UI_RGBA(148, 148, 148, 1);
            UIImageView *img = [[UIImageView alloc ] initWithFrame:CGRectMake([UIScreen width] - 40, 17, 14, 9)];
            img.image = [UIImage imageNamed:@"img_selectedcity_selected"];
            img.tag = 11;
            [cell.contentView addSubview:img];
        }else if (self.sortType == 2)
        {
            leftLabel.textColor = UI_RGBA(51, 51, 51, 1);
            leftLabel.textAlignment = NSTextAlignmentCenter;
            
            if (self.descString) {
                if (indexPath.row == self.dataList.count - 1)
                {
                    leftLabel.frame = CGRectMake(10, 3, [UIScreen width] - 20, 20);
                    
                    UILabel *redLabel = [[UILabel alloc ] initWithFrame:CGRectMake(10, 21,[UIScreen width] - 20 , 21)];
                    redLabel.font = [UIFont systemFontOfSize:14];
                    redLabel.tag = 12;
                    redLabel.textColor = [UIColor redColor];
                    redLabel.textAlignment = NSTextAlignmentCenter;
                    [cell.contentView addSubview:redLabel];
                    
                }else
                {
                    leftLabel.frame = CGRectMake(10, 0,[UIScreen width] - 20 , 44);
                }
            }
        }
    }
    
    if (_selectedIndex == indexPath.row) {
        [[cell.contentView getUIImageViewWithTag:11] setImage:[UIImage imageNamed:@"img_selectedcity_selected"]];
        [cell.contentView getUILabelWithTag:10].textColor = [UIColor blackColor];
    }else
    {
        [[cell.contentView getUIImageViewWithTag:11] setImage:nil];
    }
    [[cell.contentView getUILabelWithTag:10] setText:_dataList[indexPath.row]];
    
    if (indexPath.row == self.dataList.count - 1) {
        [[cell.contentView getUILabelWithTag:12] setText:self.descString];
    }else
    {
        [[cell.contentView getUILabelWithTag:12] setText:@""];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedIndex = indexPath.row;
    [tableView reloadData];
    [self closeBtnAction];
    
    if (self.delegate) {
         [self.delegate sortSelectedIndex:indexPath.row];
    }
   
}

-(void)show
{
    self.frame = [UIScreen bounds];
    self.bgView.alpha = 0;
    self.userInteractionEnabled = YES;
    self.sortTableView.frame = CGRectMake(0, [UIScreen height] , [UIScreen width], self.dataList.count * 44);
    
    
    [self.sortTableView reloadData];
    [self addSubview:self.bgView];
    [self addSubview:self.sortTableView];
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.window addSubview:self];
    
    WEAKSELF
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.bgView.alpha = 0.3;
        weakSelf.sortTableView.frame = CGRectMake(0, [UIScreen height] - weakSelf.sortTableView.frame.size.height, [UIScreen width], weakSelf.sortTableView.frame.size.height);
    }];
}

- (void)closeBtnAction
{
    WEAKSELF
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.sortTableView.frame = CGRectMake(0,[UIScreen height], [UIScreen width], weakSelf.sortTableView.frame.size.height);
        weakSelf.bgView.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        [weakSelf.bgView removeFromSuperview];
        [weakSelf.sortTableView removeFromSuperview];
    }];
}

-(void)selectedItem:(NSInteger)index{
    self.selectedIndex = index;
    [self.sortTableView reloadData];

}

@end
